//Defines the entry point for the console application.
//

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <iomanip.h>

struct st{
int month;
int day;
int num;
char placename[20];
int hour;
int min;
int freeseats;
}train[3];

void firstmenu(){
	cout<<"=============================Choise============================="   <<endl;
	cout<<"1.Input;"		<<endl
		<<"2.Output;"		<<endl
		<<"3.Rewrite;"	<<endl
		<<"4.Sort;"			<<endl
		<<"5.Quit;"			<<endl; 
}

void input(int n){
	cout<<"Enter the month                   :";	cin>>train[n].month;
	cout<<"Enter the day                     :";	cin>>train[n].day;
	cout<<"Enter the number of the train     :";	cin>>train[n].num;
	cout<<"Enter the name of the destination :";	cin>>train[n].placename;
	cout<<"Enter the hour                    :";	cin>>train[n].hour;
	cout<<"Enter the minute                  :";	cin>>train[n].min;
	cout<<"Enter the number of the free seats:";	cin>>train[n].freeseats;
	cout<<endl;
}

void inputstruct(){
	for(int j=0; j<3; j++)
		input(j);
}

void output(struct st* m){
	cout<<"The date of the start       :"		<<m->day<<'.'<<m->month<<endl
		<<"The number of train         :"		<<m->num<<endl
		<<"The destination             :"		<<m->placename<<endl
		<<"The time of the start       :"		<<m->hour<<':'<<m->min<<endl
		<<"The number of the free seats:"		<<m->freeseats<<endl;
	cout<<endl;
}

void outputstruct(struct st* m){
	for(int k=0; k<3; k++)
		output(m++);
}

//void outputstruct(){
//}

int l;
int hint(int l){
		cout<<"Which elememt do you want to change?";
		cin>>l;
		return l;
}

void rewrite(int p){
	if(p<sizeof(train)/sizeof(st)){
	cout<<"Enter one of the elements: train["<<p<<"] again:"<<endl;
	input(p);
	}
	else {	
		cout<<"Overrun,Please enter again!"<<endl;
		rewrite(hint(l));
	}
}

void qsort(st* a[],int left,int right)
{
	int pivot,l=left,r=right;
	pivot=(a[(left+right)/2])->freeseats;
	st* temp;
	while(l<r){    
		while( (a[r]->freeseats) > pivot ) --r;
		while( (a[l]->freeseats) < pivot ) ++l;
		if(l>=r)break;
		temp=a[l]; a[l]=a[r]; a[r]=temp;
		if(l!=pivot)--r;
		if(r!=pivot)++l;
	}
	if(l==r)l++;  
	if(left<r) qsort(a,left,l-1);
	if(l<right) qsort(a,r+1,right);
}

void sortfreeseats(){
	st* add[3];
	for(int q=0; q<3; q++)
		*(add+q)=train+q;
	qsort(add,0,2);
	for(int r=0; r<3; r++)
	{	cout<<"The date of the start       :"<<(*(add+r))->day<<'.'<<(*(add+r))->month<<endl
		    <<"The number of the train     :"<<(*(add+r))->num<<endl
		    <<"The destination             :"<<(*(add+r))->placename<<endl
		    <<"The time of the start       :"<<(*(add+r))->hour<<':'<<(*(add+r))->min<<endl
		    <<"The number of the free seats:"<<(*(add+r))->freeseats<<endl;
		cout<<endl;
	}
}



int main(int argc, char* argv[])
{
	clrscr();
	FILE* fp;
	fp=fopen("d:\\struct_train.dll","wb+");
	struct st* pt;
	pt=train;
	firstmenu();
	char i;
	while(cin>>i){
	switch(i){
	case '1' : {
		inputstruct();
		fwrite(pt,sizeof(struct st),3,fp);
		firstmenu();
		continue;
			 }
	case '2' : {
		rewind(fp);
		fread(pt,sizeof(struct st),3,fp);
		outputstruct(pt);
		firstmenu();
		continue;
			 }
	case '3' : {
		//hint(l);
		rewrite(hint(l));
		rewind(fp);
		fseek(fp,l*sizeof(struct st),SEEK_SET);
		fwrite(train+l,sizeof(struct st),1,fp);
		firstmenu();
		continue;	 
			 }
	case '4' : {
		sortfreeseats();
		firstmenu();
		continue;
			 }
/*	case '5' : {
		rewind(fp);
		fread(pt,sizeof(struct st),3,fp);
			 }*/
	case '5' : goto End;
	default: cout<<"Overrun,Please enter again!"<<endl;
	}
	}
	End:
	fclose(fp);
	getch();
	return 0;
}
